package net.sansi.v3correctserver.entity.space;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import net.sansi.v3correctserver.entity.IntPoint;
import net.sansi.v3correctserver.entity.color.CIEChroma;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.entity.screen.Rect;
import net.sansi.v3correctserver.service.camera.Camera;

import java.util.List;

/**
 * Created by Fang
 * create time : 2025/1/22
 * IDE : IntelliJ IDEA
 * <p>
 * 项目配置
 */

@Getter
@Setter
@ApiModel(description = "矫正配置")
public class CorrectCnf {

    public static final String FILE_NAME = "correct.json";

    @ApiModelProperty(value = "选择的相机名称")
    private String cameraName;

    @ApiModelProperty(value = "屏幕ID")
    private String screenId;

    @ApiModelProperty(value = "屏幕宽度")
    private Integer screenWidth = 1920;

    @ApiModelProperty(value = "屏幕高度")
    private Integer screenHeight = 1080;

    /**
     * 获取相机
     * 1. 从相机列表中找到名称为 cameraName 的相机
     *
     * @return 相机
     */
    @JsonIgnore
    public Camera getCamera() {
        if (StrUtil.isBlank(cameraName)) return null;
        return Camera.find(cameraName);
    }


    @ApiModelProperty(value = "相机参数 [红, 绿，蓝]")
    private List<Camera.Params> cameraParams = List.of(
            new Camera.Params(0).setTv(800),
            new Camera.Params(1).setTv(500),
            new Camera.Params(2).setTv(1000)
    );

    @ApiModelProperty(value = "矫正分区")
    private List<CorrectArea> areas = List.of(
            new CorrectArea(0,
                    new Rect(screenWidth, screenHeight, new IntPoint(0, 0)),
                    new Rect(screenWidth, screenHeight, new IntPoint(0, 0)))
    );

    @ApiModelProperty(value = "实际矫正范围")
    private Rect correctRange = null;

    /**
     * 获取实际矫正范围
     * 1. 如果 correctRange 为空，则返回屏幕大小的矩形
     *
     * @return 实际矫正范围
     */
    public Rect getCorrectRange() {
        if (correctRange == null) {
            correctRange = new Rect(screenWidth, screenHeight, new IntPoint(0, 0));
        }
        return correctRange;
    }

    /**
     * 通过AreaId 获取区域
     *
     * @param id 区域id
     * @return 区域
     */
    public CorrectArea getArea(int id) {
        for (CorrectArea area : areas) {
            if (area.getAreaId() == id) return area;
        }
        return null;
    }

    @ApiModelProperty(value = "是否复用照片")
    private Boolean reusePhoto = false;

    @ApiModelProperty(value = "是否复用矫正结果")
    private Boolean reuseResult = false;

    @ApiModelProperty(value = "是否修复亮暗线")
    private Boolean fixLine = true;

    @ApiModelProperty(value = "亮暗线矫正因子")
    private Double lineCorrectFactor = 2.2;

    @ApiModelProperty(value = "k 值拍摄亮度衰减(0.9表示低亮度为高亮的90%）")
    private Double kDecayFactor = 0.9;

    @ApiModelProperty(value = "亮度衰减系数, 最终矫正后的亮度衰减 [r,g,b]")
    private Double[] brightDecayFactor = new Double[]{1.0, 1.0, 1.0};

    // 打屏模式枚举
    // 自动模式（硬件优先）
    public static final int SHOW_MODE_AUTO = 0;
    // 强制软件打屏
    public static final int SHOW_MODE_SOFTWARE = 1;
    // 强制硬件打屏
    public static final int SHOW_MODE_HARDWARE = 2;

    @ApiModelProperty(value = "打屏模式： 0 自动（硬件优先）， 1 强制软件打屏， 2 强制硬件打屏")
    private Integer showMode = SHOW_MODE_AUTO;

    @ApiModelProperty(value = "打屏隔点数量")
    private Integer showSpan = 3;

    @ApiModelProperty(value = "原始色域 [红，绿，蓝]")
    private List<CIEChroma> orgChromas = List.of(
            new CIEChroma(0.6913, 0.3087, 174.56),
            new CIEChroma(0.161, 0.7226, 421.63),
            new CIEChroma(0.1266, 0.00565, 3.81),
            new CIEChroma(0.3128, 0.3292, 600.0)
    );

    @ApiModelProperty(value = "目标色域 [红, 绿，蓝]")
    private List<CIEChroma> targetChromas = List.of(
            new CIEChroma(0.6913, 0.3087, 174.56),
            new CIEChroma(0.161, 0.7226, 421.63),
            new CIEChroma(0.1266, 0.00565, 3.81),
            new CIEChroma(0.3128, 0.3292, 600.0)
    );

    @ApiModelProperty(value = "目标色温值")
    private Integer targetColorTemp = 1700;

    @ApiModelProperty(value = "目标色域表预设索引")
    private Integer targetTableIndex = 99;

    @ApiModelProperty(value = "扩展打屏像素宽度")
    private Integer showExtendWidth = 30;


}
